home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / c / GAPLib.lha / GAPLib / examples / PrisonersDilemma.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-23  |  2.2 KB  |  122 lines

  1. /*
  2.  * The iterated prisoners dilemma.
  3.  *
  4.  *
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <GAP.h>
  13.  
  14. struct Polyphant {
  15.     unsigned short int Initial;
  16.     unsigned short int Rules;
  17. };
  18.  
  19. /*
  20.  * Rules:
  21.  *
  22.  * Bit No. :  15 - 00 represents the combination of Cooperate/Defect for the
  23.  * last two games like below and the bit value is 1 = cooperate, 0 = defect. Example:
  24.  *
  25.  * DDDD DDDC DDCD DDCC DCDD DCDC DCCD DCCC CDDD CDDC CDCD CDCC CCDD CCDC CCCD CCCC
  26.  *   0    0    1    1    0    0    1    1    0    0    1    1    0    0    1    1   
  27.  *
  28.  * Initial is used to set up two hypothetical games for the first match.
  29.  *
  30.  * Scoring:
  31.  *
  32.  *           Defect  | Cooperate
  33.  *    Defect   1/1   |    5/0
  34.  *          ---------+----------
  35.  * Cooperate   0/5   |    3/3
  36.  *
  37.  */
  38.  
  39. double Play(struct Polyphant *);    /* Fitness function. */
  40.  
  41. struct Population *Pop;
  42.  
  43. static const int ScoreMatrix[2][2] = {
  44.     {3,-1},
  45.     {5,1}
  46. };
  47.  
  48. int main(int cnt,char *arg[])
  49. {
  50. int i,n;
  51. unsigned short int tft = 0x3333;
  52. struct Polyphant *Polly;
  53.  
  54. struct TagItem CreateTags[] = {
  55.     {POP_Init,RAND_INIT},
  56.     {TAG_DONE,0L}
  57. };
  58.  
  59. struct TagItem EvolveTags[] = {
  60.     {EVL_Evaluator,(IPTR)Play},
  61.     {EVL_Stats,TRUE},
  62.     {EVL_Crowding,TRUE},
  63.     {TAG_DONE,0L}
  64. };
  65.  
  66. EnterGAP(2);
  67.  
  68. InitRand(time(0));
  69.  
  70. if(cnt>1) {
  71.     n = atoi(arg[1]);
  72. }
  73.  
  74. Pop = CreatePopulation(20,sizeof(struct Polyphant),CreateTags);
  75.  
  76.  
  77. for(i=0;i<n;i++) {
  78.     Pop = Evolve(Pop,EvolveTags);
  79.     printf("Best score after %d generations: %lf\n",Pop->Generation,Play(Pop->Stat.Max));
  80.     printf("Average: %lf\n",Pop->Stat.AverageFitness);
  81. }
  82.  
  83. Polly = Pop->Stat.Max;
  84.  
  85. for(i=0;i!=16;i++) {
  86.     printf((Polly->Rules&(32768>>i))?"C":"D");
  87. }
  88. printf("\nDistance from TFT = %d\n",HammingDist(&Polly->Rules,&tft,2));
  89.  
  90. DeletePopulation(Pop);
  91.  
  92. return(0);
  93. }
  94.  
  95.  
  96. double Play(struct Polyphant *Polly)
  97. {
  98. struct Polyphant *Tweety;
  99. int last0,last1,act0,act1;
  100. int score=0;
  101. int i,n;
  102.  
  103. for(i=0;i!=Pop->NumPolys;i++) {
  104.     Tweety = PopMember(Pop,i);
  105.     last0 = Polly->Initial;
  106.     last1 = Tweety->Initial;
  107.     for(n=0;n!=16;n++) { /* 16 Matches. */
  108.         act0 = (Polly->Rules&(1<<last0))?0:1;
  109.         act1 = (Tweety->Rules&(1<<last1)?0:1);
  110.         last0<<=1;
  111.         last1<<=1;
  112.         last0|=act0;
  113.         last1|=act1;
  114.         last0&=0xf;
  115.         last1&=0xf;
  116.         score += ScoreMatrix[act0][act1];
  117.     }
  118. }
  119.  
  120. return(((double)score)/((double)Pop->NumPolys));
  121. }
  122.